//
//  oldLoginViewController.m
//  laonianbang
//
//  Created by 勇拓 李 on 17/10/10.
//  Copyright © 2017年 zaiyou. All rights reserved.
//

#import "oldLoginViewController.h"
#import "oldLabel.h"
#import "oldRegiterViewController.h"
#import "oldForgetPasswordViewController.h"
#import "oldRegex.h"
#import "AppDelegate.h"
#import "oldKeyChain.h"
#import "QNuploader.h"

@interface oldLoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *text_accountId;
@property (weak, nonatomic) IBOutlet UITextField *text_password;
@property (weak, nonatomic) IBOutlet UIButton *btn_login;
@property (weak, nonatomic) IBOutlet UIButton *btn_forget;
@property (weak, nonatomic) IBOutlet UIButton *btn_register;

@end

@implementation oldLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBarHidden = NO;
}

#pragma mark -initView
- (void)initView
{
    _btn_login.layer.cornerRadius = 6.f;
    
    [oldLabel changeLableUnderLine:_btn_register.titleLabel string:@"立即注册"];
    
    [oldLabel changePlaceholderColor:_text_accountId color:[UIColor colorWithWhite:0 alpha:0.3] string:@"请输入手机号码"];
    
    [oldLabel changePlaceholderColor:_text_password color:[UIColor colorWithWhite:0 alpha:0.3] string:@"请输入密码"];
    
    [_btn_register addTarget:self action:@selector(gotoRigster:) forControlEvents:UIControlEventTouchDown];
    [_btn_forget addTarget:self action:@selector(gotoForget:) forControlEvents:UIControlEventTouchDown];
    [_btn_login addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchDown];
}

#pragma mark - func

- (void)gotoRigster:(id)sender
{
    oldRegiterViewController *vc= [[oldRegiterViewController alloc] initWithNibName:@"oldRegiterViewController" bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)gotoForget:(id)sender
{
    oldForgetPasswordViewController *vc = [[oldForgetPasswordViewController alloc] initWithNibName:@"oldForgetPasswordViewController" bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)login:(id)sender
{
    if (![oldRegex isMobileNo:_text_accountId.text]) {
        [oldHub warningUser:@"请输入正确的手机号码"];
        return;
    }
    
    
    if (![oldRegex isValidatePassword:_text_password.text]) {
        [oldHub warningUser:@"请输入正确的密码"];
        return;
    }
    
    [oldHub waitingWindow:@"登录中.."];
    
    NSString *password = [oldRegex md5:_text_password.text];
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys: _text_accountId.text, @"mobile", password, @"password", @"ios", @"deviceType", ROLE, @"role", APPNAME, @"appName", nil];
    NSString *cid = [[NSUserDefaults standardUserDefaults] objectForKey:GT_CID];
    if (cid) {
        [dict setObject:cid forKey:@"cid"];
    }
    if ([oldKeyChain getIDFA]) {
        [dict setObject:[oldKeyChain getIDFA] forKey:@"deviceId"];
    }
    NSString* deviceName = [[UIDevice currentDevice] systemName];
    if (deviceName) {
        [dict setObject:deviceName forKey:@"deviceType"];
    }
    
    
    WeakSelf
    
    [OLDNetWork httpRequestWithPostPut:URL_Login params:dict success:^(NSDictionary *dic) {
        [OLDNetWork setToken:dic[@"token"]];
        [OLDNetWork setAFHTTPSessionManagerToken:dic[@"token"]];
        [ACCOUNT_SINGLETON initAccountByDic:dic];
        [oldHub hideHUDWindow];
        [weakSelf gotoTab];
        
    } error:^(NSString *error) {
        [oldHub warningWindow:error];
    }];
}

- (void)gotoTab
{
    [AppDelegate gotoTab];
}


@end
